#
#    对于cmake交叉编译器而言，需要考虑：
#		各类标志的填充， -I等
#
# cmake -G"MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE=../../cm/s12x.cmake ../..
# cmake -G"MinGW Makefiles" -D"PRM=x20m.prm" -DCMAKE_TOOLCHAIN_FILE=../../cm/s12x.cmake ../..
# 
# xgate OPTION: -Cstv=0x8100 -D__RUN_XGATE_OUT_OF_FLASH -CsIni0 -D__NO_FLOAT__ 
#
#  采用投机取巧的方式来增加对xgate的编译支持， 
#		1. 为 CMAKE_CXX_SOURCE_FILE_EXTENSION 增加cxgate后缀（CMakeGenericSystem.cmake中）
#		2. 定义CXX compiler为xgate编译器，重新定义CMAKE_CXX_COMPILE_OBJECT和CMAKE_CXX_CREATE_STATIC_LIBRARY
#
# 为了避免LINK_LIBRARIES中 -LPATH和linker的冲突，将LINK_DIRECTORIES(${USER_LIB_OUTPUT})删除，-l的格式换成.lib
#	指定${CW_ROOT}/proj/prm为prm文件的搜索目录， 指定-M生成map -B生成*.sx文件
#

include (CMakeForceCompiler)

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR s12x)
#set(CMAKE_OSX_ARCHITECTURES s12x)

set(S12X true)

set(CMAKE_STATIC_LIBRARY_PREFIX "")
set(CMAKE_STATIC_LIBRARY_SUFFIX ".lib")
set(CMAKE_SHARED_LIBRARY_PREFIX "")          # lib
set(CMAKE_SHARED_LIBRARY_SUFFIX ".lib")          # .so
set(CMAKE_IMPORT_LIBRARY_PREFIX )
set(CMAKE_IMPORT_LIBRARY_SUFFIX )
set(CMAKE_EXECUTABLE_SUFFIX "")          # intel hex file
set(CMAKE_LINK_LIBRARY_SUFFIX ".lib")
set(CMAKE_DL_LIBS "")

SET(CW_ROOT  "C:/Freescale/CWS12v5.1")
SET(CW_TOOLROOT  ${CW_ROOT}/prog)
SET(CW_LIB_ROOT ${CW_ROOT}/lib/hc12c)
set(CW_INC_PATH "${CW_ROOT}/lib/hc12c/include")

CMAKE_FORCE_C_COMPILER   ("${CW_TOOLROOT}/chc12.exe" MetrowerksHicross)
#CMAKE_FORCE_CXX_COMPILER ("${CW_TOOLROOT}/chc12.exe" MetrowerksHicross)
CMAKE_FORCE_CXX_COMPILER ("${CW_TOOLROOT}/cxgate.exe" MetrowerksHicross)
set(CMAKE_XGATE_COMPILER "${CW_TOOLROOT}/cxgate.exe")
set(CW_LINKER   "${CW_TOOLROOT}/linker.exe")
set(CW_BURNER   "${CW_TOOLROOT}/burner.exe")
set(CW_LIB_MAKER   "${CW_TOOLROOT}/libmaker.exe")
set(CMAKE_AR   "${CW_TOOLROOT}/libmaker.exe")

# where is the target environment 
SET(WORK_ROOT ${PROJECT_SOURCE_DIR})
SET(CMAKE_FIND_ROOT_PATH  ${CW_LIB_ROOT})

set(PRM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/proj/prm)
set(X12_ENVOPT "-ENV\"GENPATH=${CW_LIB_ROOT}/src\;${CW_LIB_ROOT}/lib\;${CW_ROOT}/lib/xgatec/lib\;${PRM_DIR}\;\" -ENV\"OBJPATH=${WORK_ROOT}/lib/s12x\" -ENV\"ABSPATH=${WORK_ROOT}/bin/s12x\" -ENV\"TEXTPATH=${WORK_ROOT}/bin/s12x\"")

#list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS "cxgate")

# CMAKE_C_FLAGS_INIT and CMAKE_EXE_LINKER_FLAGS_INIT should be set in a CMAKE_SYSTEM_PROCESSOR file
# set(CMAKE_C_FLAGS_INIT "-CPUHCS12X -D__NO_FLOAT__ -D__MAP_FLASH__ -D__RUN_XGATE_OUT_OF_FLASH -D__FAR_DATA -Mb -MapFlash -TuCuE -F2 -Lasm -WmsgNu=abcet -LmCfg=ilmu -W1 -Wpd ${X12_ENVOPT} -I${CW_INC_PATH}")
if(NOT DEFINED CMAKE_C_FLAGS_INIT)
	set(CMAKE_C_FLAGS_INIT "-CPUHCS12X -D__NO_FLOAT__ -D__MAP_FLASH__ -D__RUN_XGATE_OUT_OF_FLASH -D__FAR_DATA -Mb -MapFlash -TuCuE -F2 -Lasm -WmsgNu=abcet -LmCfg=ilmu -W1 -Wpd ${X12_ENVOPT} -I${CW_INC_PATH}")
	set(XGATE_FLAGS "-Cstv=0x8100 -D__RUN_XGATE_OUT_OF_FLASH -CsIni0 -D__NO_FLOAT__ ${X12_ENVOPT} -I${CW_INC_PATH}")
	set(LL_FLAGS "-MapFLASH -M -B ${X12_ENVOPT}")
	set(BB_FLAGS "${X12_ENVOPT} format = motorolar origin = 0x0000 len = 0xffffffff busWidth = 1 ")
endif()

if(NOT DEFINED CMAKE_EXE_LINKER_FLAGS_INIT)
	set (CMAKE_EXE_LINKER_FLAGS_INIT ${LL_FLAGS})
endif()

# compile a C file into an object file
# set(CMAKE_C_COMPILE_OBJECT  "<CMAKE_C_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
set(CMAKE_C_COMPILE_OBJECT  
	"<CMAKE_C_COMPILER> <DEFINES> <FLAGS> <SOURCE> -ObjN=<OBJECT>")

# 如果未指定PRM_NAME变量
if (NOT DEFINED PRM)
	set(PRM ${PROJECT_NAME}.prm)
endif()
	
# link object files to an executable
# CW LLFLAGS=  -M -O$(PROJECT).abs -WmsgNu=abcet -Add($(ALL_OBJ_FILES) $(ADD_OBJECTS)) $(LL_OBJ_FORMAT) $(ENVOPTIONS)
# SDCC: set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <FLAGS> <OBJECTS> -o  <TARGET> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>")
#	"${CW_LINKER} ${LL_FLAGS} <LINK_FLAGS> -Add(<OBJECTS> <LINK_LIBRARIES> -O<TARGET>.abs ${TARGET_PRM}"
# 
set(CMAKE_C_LINK_EXECUTABLE 
	"${CW_LINKER} ${LL_FLAGS} -Add(<OBJECTS> <LINK_LIBRARIES>) -O<TARGET>.abs ${PRM}"
	#"${CW_LINKER} ${LL_FLAGS} -ENV\"GENPATH=${LM_DIR}\" -Add(<OBJECTS> ${TEMP}) -O<TARGET>.abs x20m.prm"
	)

# needs 
set(CMAKE_C_CREATE_STATIC_LIBRARY "${CW_LIB_MAKER} ${X12_ENVOPT} <LINK_FLAGS> -Nopath -Cmd\"<OBJECTS> =<TARGET>\"")

set(CMAKE_CXX_COMPILE_OBJECT  "<CMAKE_CXX_COMPILER>  <DEFINES> ${XGATE_FLAGS} <FLAGS> <SOURCE> -ObjN=<OBJECT>")
set(CMAKE_CXX_CREATE_STATIC_LIBRARY "${CW_LIB_MAKER} ${X12_ENVOPT} <LINK_FLAGS> -Nopath -Cmd\"<OBJECTS> =<TARGET>\"")

